package com.rpoonolly.simplegame.processors;

import java.util.List;

import com.rpoonolly.simplegame.Config;
import com.rpoonolly.simplegame.core.CollisionDetector;
import com.rpoonolly.simplegame.core.CollisionResponder;
import com.rpoonolly.simplegame.core.aabb.AABBCollision;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.core.aabb.DynamicAABBEntity;
import com.rpoonolly.simplegame.core.aabb.EntityGameMap;

public class GhostProcessor extends SimpleNPCProcessor {
	public GhostProcessor(
		CollisionDetector<AABBEntity, AABBCollision> detector,
		CollisionResponder<AABBCollision> responder,
		EntityGameMap<AABBEntity> gameMap
	) {
		super(detector, responder, gameMap);
	}
	
	@Override
	public void process() {
		processTime();
		if (Config.isEditModeEnabled.get()) return;
		
		int ghostCenterX, ghostCenterY, playerCenterX, playerCenterY;
		
		AABBEntity player = (DynamicAABBEntity) entities.get(Config.ENT_GRP_PLAYER).get(0);
		List<AABBEntity> ghosts = entities.get(Config.ENT_GRP_GHOSTS);
		
		playerCenterX = player.getX() + (player.getWidth() / 2);
		playerCenterY = player.getY() + (player.getHeight() / 2);
		
		for(AABBEntity ghost : ghosts) {
			ghostCenterX = ghost.getX() + (ghost.getWidth() / 2);
			ghostCenterY = ghost.getY() + (ghost.getHeight() / 2);
			
			if (Config.GHOST_CHASE_RADIUS < Math.abs(ghostCenterX - playerCenterX) ||
					Config.GHOST_CHASE_RADIUS < Math.abs(ghostCenterY - playerCenterY))
				continue;
			
			processNPCMoveLogic((DynamicAABBEntity) ghost, collidables, player.getX(), player.getY(), Config.ENEMY_MAX_SPEED);
		}
	}
}
